package com.demon.birth.util;

import java.io.File;

import com.demon.birth.R;
import com.demon.birth.event.Callback;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class ThumbImageDialog extends DialogFragment implements Callback<String> {
	private Bitmap bm;
	private String path;

	public ThumbImageDialog(Bitmap bm) {
		this.bm = bm;
	}

	public ThumbImageDialog(String path) {
		File f = new File(path);
		f = new File(f.getParent() + "big." + f.getName());
		if (f.exists()) {
			this.path = f.getPath();
		} else {
			this.path = path;
		}
		Log.d("ThumbImageDialog", this.path);
		this.bm = BitmapFactory.decodeFile(this.path);
	}

	Callback<ThumbImageDialog> td;

	public ThumbImageDialog(String path, Callback<ThumbImageDialog> td) {
		this(path);
		this.td = td;
	}

	public ThumbImageDialog(Callback<ThumbImageDialog> td) {
		// this(path);
		this.td = td;
	}

	public String getPath() {
		return path;
	}

	public ThumbImageDialog() {

	}

	private ZoomImageView img;

	public View getImageView() {
		return img;
	}

	public void setImageBitmap(Bitmap b) {
		img.setImageBitmap(b);
	}

	public void setImageBitmap(String path) {
		this.path = path;
		Log.d("ThumbImageDialog.setImageBitmap", (img == null) + "=" + (bm == null) + "=" + path);
		this.bm = BitmapFactory.decodeFile(path);
		if (img != null) {
			setImageBitmap(bm);
		}
	}

	@Override
	public void show(FragmentManager manager, String tag) {
		super.show(manager, tag);
	}

	@Override
	public void onStart() {
		super.onStart();
		if (td != null) {
			td.back(path.indexOf("/big.") < 0, this);
			Log.d("ThumbImageDialog.onStart", path);
		}
	}

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Log.v("onCreateDialog", "onCreateDialog");
		LayoutInflater mInflater = LayoutInflater.from(getActivity());
		Dialog dialog = new Dialog(getActivity(), R.style.FullScreenDialog);
		View view = mInflater.inflate(R.layout.image_details, null);
		img = (ZoomImageView) view.findViewById(android.R.id.icon);
		if (on != null) {
			CheckBox cb = (CheckBox) view.findViewById(android.R.id.checkbox);
			cb.setVisibility(View.VISIBLE);
			cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					Checked = isChecked;
				}
			});
		}
		img.setImageBitmap(bm);
		img.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dismiss();
				if (on != null)
					on.onCheckedChanged(obj, Checked);
			}
		});
		// Log.v("onCreateDialog->", "onCreateDialog:" + bm.getHeight());
		// PopupWindow popupWindow = new PopupWindow(view,
		// LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		dialog.setContentView(view);
		return dialog;
	}

	private boolean Checked = true;
	private Object obj;

	public void setOnCheckedChangeListener(OnCheckedChangeListener l, Object t) {
		this.on = l;
		obj = t;
	}

	private OnCheckedChangeListener on;

	public interface OnCheckedChangeListener {
		public void onCheckedChanged(Object image, boolean isChecked);
	}

	@Override
	public boolean back(boolean b, String t) {
		Log.d("ThumbImageDialog.back", "b is " + b + ",t = " + t);
		if (b) {
			setImageBitmap(t);
		}
		return false;
	}
}
